<!--  -->
<template>
  <editor :init="init" v-model="content" v-if="pre"></editor>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
// eslint-disable-next-line no-unused-vars
// 引入核心
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import { plugins, toolbar, fontsize_formats, font_formats } from "./save";
import "./load";
import OSS from "ali-oss";
export default {
  name: "a-tinymce",
  model: {
    prop: "value",
    event: "change",
  },
  props: {
    value: {
      type: String,
    },
    height: {
      type: Number,
      default: 800,
    },
  },
  //import引入的组件需要注入到对象中才能使用
  components: { Editor },
  data() {
    //这里存放数据
    return {
      pre: false,
      content: this.value,
      init: {
        language: "zh_CN",
        plugins: plugins,
        toolbar: toolbar,
        height: this.height,
        fontsize_formats: fontsize_formats,
        font_formats: font_formats,
        // 开启自定义上传
        automatic_uploads: true,
        // 自定义上传
        images_upload_handler: this.images_upload_handler,
        paste_data_images: true,
        // 复制图片行为监听
        // urlconverter_callback: this.urlconverter_callback,
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    value(data) {
      this.content = data;
    },
    content(data) {
      this.$emit("change", data);
      this.$emit("changeValue", data);
    },
  },
  //方法集合
  methods: {
    /**
     * 复制图片行为监听
     * @param {*} url
     * @param {*} node
     * @param {*} on_save
     * @param {*} name
     */
    async urlconverter_callback(url, node, on_save, name) {
      console.log(url, node, on_save, name);
      if (
        (node === "img" && url.startsWith("blob:")) ||
        url.startsWith("data:")
      ) {
        // 复制图片操作让它转到正常上传图片行为去
        tinymce.activeEditor && tinymce.activeEditor.uploadImages();
      }
      return url;
    },
    /**
     * 图片定制上传
     * @param {*} blobInfo
     * @param {*} success
     * @param {*} failure
     */
    async images_upload_handler(blobInfo, success, failure) {
      console.log(success);
      let blob = blobInfo.blob();
      if (!success || !blob) {
        return;
      }
      const reader = new FileReader();
      reader.readAsArrayBuffer(blob);
      let that = this;
      reader.onload = async function (event) {
        // arrayBuffer转Buffer
        const buffer = new OSS.Buffer(event.target.result);
        let result = await that.$commonjs.upload.uploadSingle(
          buffer,
          blob.type.split("/")[1]
        );
        if (result) {
          success(that.$filePrefix + result.uuid);
        } else {
          console.log("富文本上传到接口返回错误");
          failure("上传图片失败");
        }
      };
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    tinymce.init({});
    this.$nextTick(() => {
      this.pre = true;
    });
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style>
</style>